#include "WheelZoom.h"
#include <QWheelEvent>
#include "MainPlot.h"

//-------------------------------------

WheelZoom::WheelZoom( MainPlot * p ) :
    plot( 0 )
{
    plot = p;
    p->installEventFilter( this );
}

//-------------------------------------

bool WheelZoom::eventFilter( QObject * target, QEvent * e )
{
    if( target == plot )
    {
        if( e->type() == QEvent::Wheel )
        {
            handleWheel( e );
            //zoom->updatePlot( wheelZoomRect );
        }
    }

    return QObject::eventFilter( target, e );
}

//-------------------------------------

void WheelZoom::handleWheel( QEvent * event )
{
    QWheelEvent * wEvent = static_cast< QWheelEvent* >( event );

    int wd = wEvent->delta();

    double kw = 1.2 * wd / 120;

    if( wd )
    {
        QwtScaleMap smY = plot->canvasMap( plot->AxisY() );

        double my = ( smY.s1() + smY.s2() ) / 2;
        double dy = ( smY.s2() - smY.s1() ) / 2;

        if( wd > 0 )
            dy /= kw;
        else
            dy *= -kw;

        plot->setAxisScale( plot->AxisY(), my-dy, my+dy );

        QwtScaleMap smX = plot->canvasMap( plot->AxisX() );

        double mx = ( smX.s1() + smX.s2() ) / 2;
        double dx = ( smX.s2() - smX.s1() ) / 2;

        if( wd > 0 )
            dx /= kw;
        else
            dx *= -kw;

        plot->setAxisScale( plot->AxisX(), mx-dx, mx+dx );

        wheelZoomRect = QRectF( smX.s1(), smY.s2(), smX.s2()-smX.s1(), smY.s2()-smY.s1() );

        plot->replot();
    }
}

//-------------------------------------



